﻿/*
 * @Description:客源列表页页面js
 * @Author:zb,yangpl
 * @Date:
 * @Date:2015-01-03
 */
$(function () {
    var tabObj = $('#gdIndexCustomerList th');
    //插入跟进列表

    $("#gdIndexCustomerList .js-follow").unbind('click');
    $("#gdIndexCustomerList .js-follow").bind("click", function () {
        var currentElement = $(this);
        var columnCount = currentElement.attr('thcount');
        var row = $(this).parent().parent().parent();
        var isOpenCurrentFollow = $(this).attr("follow-open");
        if (isOpenCurrentFollow == "true") {
            row.next().remove();
            $(this).attr("follow-open", "false");
            return;
        }

        var currRowIndex = row[0].rowIndex;
        var keyId = currentElement.attr('keyid');
        var followType = currentElement.attr('followType');
        var reqData = {
            'followType': followType,
            'ColumnCount': columnCount,
            'customerKeyId': keyId
        };
        $.AjaxAgency("/customer/inquiry-follow-for-list", reqData, function (data) {
            if (data.indexOf('pageDiscription="我是错误页"') > -1) {
                $.OKDialogAgency('<div class="tc">请求失败！</div>');
            } else {
                if (currentElement.attr("follow-open") != "true") {
                    currentElement.css("background-position", "-127px -189px");
                    //$("#gdIndexCustomerList > tbody > tr").eq(currRowIndex).after(data);

                    $("#gdIndexCustomerList > tbody > tr").eq(currRowIndex).after(data);
                    //$("#followPartialTable_" + keyId + " div").foldLine(false, 0);
                    currentElement.attr("follow-open", "true");
                }

            }
        });

    });

    //排序逻辑
    if ($('#hiddenSortValue').val() != '') {
        for (var i = 0; i < tabObj.length; i++) {
            if ($(tabObj[i]).attr('callValue')) {
                if ($(tabObj[i]).attr('callValue').split('-')[0] == $('#hiddenSortValue').val().split('-')[0]) {

                    if ($('#hiddenSortValue').val().split('-')[1] == 'asc') {
                        $(tabObj[i].childNodes[0]).attr('class', 'sort-up');
                        $(tabObj[i]).attr('callValue', $('#hiddenSortValue').val().split('-')[0] + '-desc');
                        break;
                    }
                    $(tabObj[i].childNodes[0]).attr('class', 'sort-down');
                    $(tabObj[i]).attr('callValue', $('#hiddenSortValue').val().split('-')[0] + '-asc');
                    break;
                }
            }
        }
    }
    $('#gdIndexCustomerList th').click(function () {
        var resultVal = $(this).attr('callValue');
        if (resultVal) {
            $('#hiddenSortValue').val(resultVal);
            $('#searchForm').submit();
        }
    });

    /*备注鼠标滑过弹层*/
    $(".icon-remark").hover(function () {
        if ($('#remark' + $(this).attr('propertyKeyid')).html().length > 3) {
            $("#remark" + $(this).attr("propertyKeyid")).show();
        }

    }, function () {
        $("#remark" + $(this).attr("propertyKeyid")).hide();
    });

    //看客户
    $(".see-owner").bind("click", function () {
        $.dialog({
            id: "seeOwner",
            title: "看客户",
            lock: true,
            background: "#808080",
            opacity: 0.87,
            content: $("#seeOwner").html()
        });
        var urlNum = $(this).attr("keyid");
        $.ajax({
            type: 'post',
            url: '/Customer/inquiry-contacts-find',
            data: { 'KeyId': urlNum },
            success: function (data) {
                $("#seeOwnerdiv").html(data);
                $("#writeFollowHref").attr("href", "/customer/inquiry-details?keyId=" + urlNum + "#maodian");

            }
        });

    });

});
//潜客洗客池中跟进图标按钮
$(".followPrePubCus").unbind('click');
$(".followPrePubCus").bind("click", function () {
    var obj = $(this)
    //防重复提交
    var opFlag = obj.parents(".op").attr("data-flag");
    if (obj.hasClass("setcolor") || opFlag != "true") {
        return;
    }
    obj.parents(".op").attr("data-flag", "false");
    var currentElement = $(this);
    var keyId = currentElement.attr('keyid');
    var customerKeyId = currentElement.attr('custkeyid');
    var content = currentElement.attr('title');
    var custname = currentElement.attr('custname');
    var followType = currentElement.attr("isturnprivate");
    var followCode = currentElement.attr("value");
    var mobile = currentElement.attr('mobile');
    var type = obj.text();
    var reqData = {
        'InquiryKeyId': keyId,
        'FollowTypeKeyId': followCode,
        'Content': content,
        'IsPrePub': true,
        'Mobile': mobile,
        'CustomerName': custname,
        'SourceObjectKeyId': customerKeyId,
        'Type': type
    };
    //debugger;
    //return;
    if (followType != "5") {
        $.AjaxAgency("/customer/inquiry-other-follow-add-ajax", reqData, function (data) {
            if (data != "ok") {
                $.OKDialogAgency('<div class="tc">' + data.ErrorMsg + '</div>');
                $("#createPreCustFollow").removeAttr("disabled");
                obj.parents(".op").attr("data-flag", "true");
                return false;
            }

            var message = "跟进添加成功！";
            $.dialog({
                id: "dialogAddPrecustFollow",
                content: '<div class="tc"></div>' + message + '</div>',
                close: function () {
                    window.location.reload();
                }
            });
            setTimeout(function () {
                art.dialog.get('dialogAddPrecustFollow').close();
            }, 1000);
        });
    }
    else {
        reqData.FollowType = followType;
        reqData.CustKeyId = customerKeyId;
        $.AjaxAgency("/customer/precust-page-follow", reqData, function (data) {
            if (data) {
                $.dialog({
                    id: "dialogPreCustFollow",
                    title: "跟进内容",
                    width: 700,
                    lock: true,
                    content: data
                });
                $("#PreCustName").val(currentElement.attr("custName"));
                var selectPhones = currentElement.parent().parent().find("td.phone").text().trim().split(',');
                var offSwitch = $("#hiddenOffSwitch").val();
                if (offSwitch == 1) {
                    var b = new Base64();
                    var rel = (b.decode(currentElement.parent().parent().find("span.sprealnum").text())).split(',');
                    for (var i = 0; i < selectPhones.length; i++) {
                        $("#PreCustTel").append("<option value='" + b.encode(rel[i]) + "'>" + selectPhones[i] + "</option>");
                    }
                }
                else {
                    for (var i = 0; i < selectPhones.length; i++) {
                        $("#PreCustTel").append("<option value='" + selectPhones[i] + "'>" + selectPhones[i] + "</option>");
                    }
                }
                $.inputStatistics2($("div .aui_content #txtContent"));
                agencyVerify.notNullInput("#createPreCustFollowForm", "#createPreCustFollowForm input[isnull=isNull]:visible", true, "blur");
                agencyVerify.nonnegativeIntInput("input[isint=1]", true);
                agencyVerify.bigOrSmallCompareAlert("#createPreCustFollowForm", "#RentPriceFrom:visible", "#RentPriceTo:visible", "最高价格不能低于最低价格", true);
                agencyVerify.bigOrSmallCompareAlert("#createPreCustFollowForm", "#BuyPriceFrom:visible", "#BuyPriceTo:visible", "最高价格不能低于最低价格", true);
                agencyVerify.notdefaultSelect("#createPreCustFollowForm", "#BuyReasonKeyId:visible", -1, "必须选择购房原因！", true);
                agencyVerify.notCheckedRadio("#createPreCustFollowForm", '#GenderKeyId input[type=radio]', "请选择客户性别");
                agencyVerify.notCheckedRadio("#createPreCustFollowForm", '#consignInfo input[type=radio]', "请选择客户意向");
                obj.parents(".op").attr("data-flag", "true");
            }
        });
    }
});

